package eng.agile;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends MapActivity{

	static EditText e1;
	static EditText e2;
	String username;
	String password;
	Boolean checkbool = true;
	MapView m;
	public MapController mc;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		m = (MapView) findViewById(R.id.mapView);
		m.setBuiltInZoomControls(true);
		mc = m.getController();
		mc.setZoom(3);
		performLoginWork();
	}

	public void performLoginWork(){
		e1 = (EditText)findViewById(R.id.editText1);
		e2 = (EditText)findViewById(R.id.editText2);

		final Builder builder = new AlertDialog.Builder(this);
		final Button clear = (Button) findViewById(R.id.button1);
		clear.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				e1.setText("");
				e2.setText("");
			}
		});

		final Button login = (Button) findViewById(R.id.button2);
		login.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				if( e1.getText().length() == 0 || e2.getText().length() == 0){
					if( e1.getText().length() == 0 && e2.getText().length() == 0){
						builder.setMessage("Try to provide Username and Password\n---TRY AGAIN !---")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();

							}
						});
						e1.setText("");
						e2.setText("");
					}
					else if( e1.getText().length() == 0 ){
						builder.setMessage("Try to provide Username to login\n---TRY AGAIN !---")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
						e1.setText("");

					}

					else if( e2.getText().length() == 0 ){
						builder.setMessage("Try to provide Password to login\n---TRY AGAIN !---")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
						e2.setText("");

					}
					checkbool = false;
					AlertDialog dialog = builder.create();
					dialog.show();
				}
				else
				{
					username = e1.getText().toString();
					password = e2.getText().toString();
					username.trim();
					password.trim();
					Pattern p = Pattern.compile("[\\W | \\d]");
					Pattern p2 = Pattern.compile("[\\D]");
					Matcher m2 = p.matcher(username);
					Matcher m = p.matcher(username);
					if(m.find())
					{
						builder.setMessage("Username must contain only Alphabelts\n---TRY AGAIN !---")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
						e1.setText("");
						e2.setText("");
						checkbool = false;
						AlertDialog dialog = builder.create();
						dialog.show();
					}
					else{
						Intent i1 = new Intent(LoginActivity.this,TrakerActivity.class);
						startActivityForResult(i1,0);
					}
				}
			}
		});
	}

	public boolean testtrue(String s1, String s2){
		e1 = (EditText)findViewById(R.id.editText1);
		e2 = (EditText)findViewById(R.id.editText2);
		e1.setText(s1);
		e2.setText(s2);
		checkbool =true;
		performLoginWork();
		return checkbool;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}


}
